var A = getApp(), e = A.globalData.url, t = require("../../../utils/util.js");

Page({
    data: {
        domain: e,
        shopId: null,
        carNo: [],
        carId: [],
        carIndex: 0,
        serviceList: [],
        serviceId: [ 1, 2 ],
        serviceIndex: 0,
        isQueue: !1,
        nowIndex: 0,
        carExist: !1,
        coupons: [],
        image: "",
        serviceSelected: !1,
        selectTime: "",
        DaystartTime: "09:10",
        DayendTime: "05:00"
    },
    onLoad: function(A) {
        var e = this, t = new Date();
        t.setTime(t.getTime() + 864e5);
        var a = t.getFullYear() + "-" + (t.getMonth() + 1 < 10 ? "0" + (t.getMonth() + 1) : t.getMonth() + 1) + "-" + (t.getDate() < 10 ? "0" + t.getDate() : t.getDate()), d = new Date();
        d.setTime(t.getTime() + 2592e6);
        var i = d.getFullYear() + "-" + (d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1) + "-" + (d.getDate() < 10 ? "0" + d.getDate() : d.getDate());
        e.getImage();
        var n = A.service, r = A.home, o = A.serviceName;
        this.setData({
            selectDate: a,
            selectEndDate: i,
            shopId: A.shopId,
            serviveName: o,
            service: A.service,
            selectServiceId: A.selectServiceId,
            serviceType: A.serviceType,
            home: r
        }), this.initCar(), this.getUserInfo(), 0 == n ? wx.setNavigationBarTitle({
            title: "排队"
        }) : wx.setNavigationBarTitle({
            title: "预约"
        }), "" == o || void 0 === o || "" == r ? (e.setData({
            serviceSelected: !0
        }), e.initService(A.serviceType)) : e.getCouponEnableInfo();
    },
    onShow: function() {
        var A = this;
        A.getQueueState(), A.initCar(), A.getImage();
        var e = A.data.carNo[A.data.carIndex];
        void 0 === A.data.phone || void 0 === e ? setTimeout(function() {
            A.getCardInfo();
        }, 1e3) : A.getCardInfo();
    },
    getQueueState: function() {
        var e = this;
        0 == e.data.service && t.request(A, "wx/cust/getOneQueueByShopId", "POST", {
            shopId: e.data.shopId,
            token: A.globalData.token,
            state: 0
        }, function(A) {
            A.data.state ? 1 == A.data.data.state ? e.setData({
                isQueue: !0,
                state: "服务中",
                endTime: A.data.data.timeEnd,
                queueOrderId: A.data.data.queueOrderId,
                nowIndex: A.data.data.nowIndex
            }) : 0 == A.data.data.state || 2 == A.data.data.state ? e.setData({
                isQueue: !0,
                state: "排队中",
                addTime: A.data.data.timeStart,
                queueOrderId: A.data.data.queueOrderId,
                nowIndex: A.data.data.nowIndex
            }) : e.setData({
                isQueue: !1,
                state: "",
                queueOrderId: A.data.data.queueOrderId
            }) : "你没有排队" == A.data.message && e.setData({
                isQueue: !1,
                state: ""
            });
        }, function(A) {});
    },
    cancel: function(e) {
        var a = this;
        t.request(A, "wx/cust/deletequeue", "POST", {
            token: A.globalData.token,
            queueOrderId: a.data.queueOrderId
        }, function(A) {
            A.data.state ? (wx.showToast({
                title: "取消成功"
            }), setTimeout(function() {
                wx.navigateBack();
            }, 1e3)) : wx.showToast({
                title: "取消失败",
                icon: "none"
            });
        }, function(A) {});
    },
    onPullDownRefresh: function() {
        this.getQueueState(), wx.showNavigationBarLoading(), setTimeout(function() {
            wx.hideNavigationBarLoading(), wx.stopPullDownRefresh(), wx.showToast({
                title: "刷新成功",
                icon: "none"
            });
        }, 1500);
    },
    bindDateChange: function(e) {
        this.setData({
            date: e.detail.value
        });
        A.globalData.token;
    },
    bindTimeChange: function(A) {
        var e = this.data.date + "  " + A.detail.value + ":00";
        this.setData({
            time: A.detail.value,
            selectTime: e
        });
    },
    initCar: function() {
        var e = this, a = A.globalData.token;
        t.request(A, "wx/car/list?token=" + a, "GET", {}, function(A) {
            if (A.data.length > 0) {
                var t = A.data, a = [], d = [];
                for (var i in t) a[i] = t[i].carId, d[i] = t[i].carNo;
                e.setData({
                    carExist: !0,
                    carId: a,
                    carNo: d
                });
            } else e.setData({
                carExist: !1
            }), setTimeout(function() {
                wx.navigateTo({
                    url: "/pages/info/page/addChe/addChe?state=0"
                });
            }, 1e3);
            "" != e.data.serviveName && void 0 !== e.data.serviveName && void 0 !== e.data.phone || setTimeout(function() {
                e.getCardInfo();
            }, 1e3);
        }, function(A) {});
    },
    bindCarChange: function(A) {
        this.setData({
            carIndex: A.detail.value
        });
    },
    paiduis: function(A) {
        var e = this, t = this.data.carExist, a = this.data.carId[this.data.carIndex], d = this.data.selectServiceId, i = this.data.serviceType, n = this.data.service, r = this.data.shopId;
        if ("" != a && null != a && "" != d && null != d && 0 != t) if ("0" == e.data.service) wx.redirectTo({
            url: "/pages/home/paidui/paidui?shopId=" + r + "&carId=" + a + "&serviceId=" + d + "&serviceType=" + i + "&service=" + n + "&formId=" + e.data.formId
        }); else {
            var o = e.data.time;
            "" != o && null != o && wx.redirectTo({
                url: "/pages/home/paidui/paidui?shopId=" + r + "&carId=" + a + "&serviceId=" + d + "&serviceType=" + i + "&serviceTime=" + o + "&service=" + n + "&selectTime=" + e.data.selectTime + "&formId=" + e.data.formId
            });
        } else 0 == t ? wx.showToast({
            title: "请添加车辆：个人中心--\x3e车辆信息",
            icon: "none"
        }) : wx.showToast({
            title: "缺少相关数据",
            icon: "none"
        });
    },
    getUserInfo: function() {
        var e = this, a = A.globalData.token;
        t.request(A, "wx/cust/getUserInfo", "POST", {
            token: a
        }, function(A) {
            e.setData({
                phone: A.data.phone
            });
        }, function(A) {});
    },
    getCardInfo: function() {
        var e = this, a = A.globalData.token, d = e.data.carNo[e.data.carIndex];
        "1" == e.data.serviceType && (void 0 === e.data.phone || void 0 === d ? setTimeout(function() {
            var d = e.data.carNo[e.data.carIndex];
            t.request(A, "wx/cust/getWxCustCardByPhoneAndCustId", "GET", {
                token: a,
                shopId: e.data.shopId,
                carNo: d,
                phone: e.data.phone,
                cardType: 0 == e.data.service ? "" : 2
            }, function(A) {
                if (A.state) {
                    var a = A.data;
                    null != a && 0 != a.length && (a.forEach(function(A) {
                        A.remainNum = t.convertMath(A.remainNum);
                    }), e.setData({
                        cardList: a
                    }));
                }
            }, function(A) {});
        }, 1e3) : t.request(A, "wx/cust/getWxCustCardByPhoneAndCustId", "GET", {
            token: a,
            shopId: e.data.shopId,
            carNo: d,
            phone: e.data.phone,
            cardType: 0 == e.data.service ? "" : 2
        }, function(A) {
            if (A.state) {
                var a = A.data;
                null != a && 0 != a.length && (a.forEach(function(A) {
                    A.remainNum = t.convertMath(A.remainNum);
                }), e.setData({
                    cardList: a
                }));
            }
        }, function(A) {}));
    },
    getCouponEnableInfo: function() {
        var e = this, a = A.globalData.token;
        e.data.carNo[e.data.carIndex];
        t.request(A, "wx/mkt/getmktCouponEnable", "GET", {
            token: a,
            shopId: e.data.shopId,
            servicePackageId: e.data.selectServiceId
        }, function(A) {
            var a = A.data;
            a.forEach(function(A) {
                A.deductAmountAmount = t.convertMath(A.deductAmountAmount), A.minPayAmount = t.convertMath(A.minPayAmount);
            }), e.setData({
                coupons: a,
                length: a.length
            });
        }, function(A) {});
    },
    bindServiceChange: function(A) {
        var e = this, t = A.detail.value;
        this.setData({
            serviceIndex: A.detail.value,
            selectServiceId: e.data.serviceId[t],
            serviveName: e.data.serviceList[t]
        });
    },
    initService: function(e) {
        var a = this;
        A.globalData.token;
        t.request(A, "wx/biz/getListByShopId", "POST", {
            shopId: a.data.shopId,
            serviceType: e,
            token: A.globalData.token
        }, function(A) {
            if (A.data.length > 0) {
                var e = A.data, t = [], d = [];
                for (var i in e) t[i] = e[i].servicePackageId, d[i] = e[i].servicePackageName;
                a.setData({
                    serviceId: t,
                    serviceList: d,
                    selectServiceId: e[0].servicePackageId,
                    serviveName: e[0].servicePackageName
                });
            } else wx.showToast({
                title: "该店铺没有服务",
                icon: "none"
            });
        }, function(A) {});
    },
    onUnload: function() {
        this.data.isBack && wx.navigateBack();
    },
    getImage: function() {
        var e = this, a = A.globalData.token;
        t.request(A, "wx/sys/getCardsUrl", "GET", {
            token: a
        }, function(A) {
            A.state && e.setData({
                img1: A.data[0].url,
                img2: A.data[1].url
            });
        }, function(A) {});
    },
    formSubmit: function(A) {
        var e = this;
        "the formId is a mock one" != A.detail.formId ? (e.setData({
            formIdString: A.detail.formId + "," + e.data.formIdString,
            formId: A.detail.formId
        }), e.paiduis()) : (e.setData({
            formId: ""
        }), e.paiduis());
    }
});